**************************************************************************
*Goal:analysis

*Data:   pf_all_wide.dta             pf_all_long.dta

*output：正式分析前生成 temp_a.dta
**************************************************************************
    clear all
    set maxvar 10000
	set matsize 10000
	set more off

	/* Load data */ 
	
	 cd ""
	global workingdir ""
	global tabledir    ""
	use "/temp_a_clean.dta",clear



	* Set globals: 
	* Set global definition for control variables:
	global base_vars      age_in_month_0 gender_3  premature_0  birth_order_0 bl_cog_delay_sd_0 bl_lang_delay_sd_0 bl_motor_delay_sd_0  se_problem_0 edu_mom_dummy_0 pri_caregiver_mom_0 dibao_0 wealth_0  
	global control        age_in_month_0 gender_3  premature_0  birth_order_0  age_mom_delivery_0  pri_caregiver_par_3 edu_mom_dummy_0  dibao_0    
	global skill_base      Bayley_cog_rscore_st_0 Bayley_lan_rscore_st_0 Bayley_mot_rscore_st_0	 asqse_score_cut_r_st_0
    global skill_base_de   bl_cog_delay_sd_0 bl_lang_delay_sd_0 bl_motor_delay_sd_0 se_problem_0 
	
	* Set global definition for Mediating variables:
	global ppractice      f12_3 f13_3 f14_3 f15_3 f16_3 f17_3
	global ppractice_fa   f12_father_3 f13_father_3 f14_father_3 f15_father_3 f16_father_3 f17_father_3	
	global dass           depression_de_3 anxiety_de_3 stress_de_3
	global investment     investment_book_3 investment_toy_3 investment_vitamin_3 investment_study_3
	global dass_base      depression_de_0 anxiety_de_0 stress_de_0

	* Set global definition for dependent variables:
    global wppsi_st_3      wppsi_FSIQ_raw_st_3	 wppsi_VCI_raw_st_3	  wppsi_VSI_raw_st_3   wppsi_FRI_raw_st_3  wppsi_WMI_raw_st_3	wppsi_PSI_raw_st_3 
	global wppsi_delay_3   wppsi_FSIQ_delay_3   wppsi_VCI_delay_3  wppsi_VSI_delay_3 wppsi_FRI_delay_3   wppsi_WMI_delay_3  wppsi_PSI_delay_3
    global sdq_score_st    rebdtot_st_3    rprosoc_st_3   remotion_st_3  rconduct_st_3  rhyper_st_3  rpeer_st_3 
	global sdq_de          pebdtot_problem_3    pprosoc_problem_3  pemotion_problem_3 pconduct_problem_3 phyper_problem_3    /// 
	                        ppeer_problem_3
 

/*------------------------------------------------------------------------------
Table 1 Descriptive Statistics and Balance Check of Sampling Children and Families
------------------------------------------------------------------------------*/
	
	
	* Joint test Table 1:
	probit treatment_0 $base_vars 
	testparm $base_vars

	
	*** Table 1a: 
	orth_out $base_vars  using "$tabledir/balance_pre.xls" if valid0==1, by(treatment_0) vce(cluster villid_0) ///
			 pcompare count se	numlabel armlabel(0 "控制组" 1 "干预组") title(Balance Table\label{tab1})  replace  full
	
    sum $base_vars if valid0==1
	




/*------------------------------------------------------------------------------
TABLE 2-5: Average Treatment Effects - ITT 
------------------------------------------------------------------------------*/	

global Y_main ///
    $wppsi_st_3 $wppsi_delay_3 ///
    $sdq_score_st $sdq_de

global X_main ///
    treatment_0 ///
    $skill_base $skill_base_de ///
    $control ///
    countyid_0 cohort_0 villid_0 ///
    wppsi_tid inter_id


local KEEP ""

* 先把两个global拼成一个list，再遍历
local allvars "$Y_main $X_main valid3"

foreach v of local allvars {
    cap confirm variable `v'
    if _rc==0 local KEEP "`KEEP' `v'"
}

di "`KEEP'"


cap drop __miss_main paper_sample_main
egen __miss_main = rowmiss(`KEEP')
gen paper_sample_main = (valid3==1 & __miss_main==0)

tab paper_sample_main
count if paper_sample_main==1
	
	
***table 2& table 4 cognitive	
	eststo  clear 
		foreach var of varlist $wppsi_st_3{
			eststo clear

			qui	eststo OLS: 	 	reg   	`var'  	treatment_0 $skill_base	                  i.countyid_0  i.wppsi_tid i.cohort_0  if paper_sample_main==1, vce(cluster villid_0)  // OLS
			qui	eststo OLSwCont: 	reg   	`var'   treatment_0 $skill_base	  $control	      i.countyid_0  i.wppsi_tid i.cohort_0  if paper_sample_main==1, vce(cluster villid_0) // OLS + controls

			esttab using "$tabledir/OLS_cog.csv",   b(3)  se(3) obslast star(* 0.10 ** 0.05 *** 0.01) ///
				keep(treatment_0) ar2 mtitles nonotes title(`var') 	append
			  }
	
	
	eststo  clear 
		foreach var of varlist $wppsi_delay_3{
			eststo clear

			qui	eststo OLS: 	 	reg   	`var'  	treatment_0 $skill_base_de	                  i.countyid_0  i.wppsi_tid i.cohort_0 if paper_sample_main==1, vce(cluster villid_0)  // OLS
			qui	eststo OLSwCont: 	reg   	`var'   treatment_0 $skill_base_de	  $control	      i.countyid_0  i.wppsi_tid i.cohort_0 if paper_sample_main==1, vce(cluster villid_0) // OLS + controls

			esttab using "$tabledir/OLS_cog_delay.csv",   b(3)  se(3) obslast star(* 0.10 ** 0.05 *** 0.01) ///
				keep(treatment_0) ar2 mtitles nonotes title(`var') 	append
			  }
	

***table 3& table 5 non-cognitive	

		
			eststo  clear 
		foreach var of varlist $sdq_score_st{
			eststo clear

			qui	eststo OLS: 	 	reg   	`var'  	treatment_0 $skill_base	                      i.countyid_0   i.cohort_0 i.inter_id if paper_sample_main==1, vce(cluster villid_0)  // OLS
			qui	eststo OLSwCont: 	reg   	`var'   treatment_0 $skill_base	     $control	      i.countyid_0   i.cohort_0 i.inter_id if paper_sample_main==1, vce(cluster villid_0) // OLS + controls

			esttab using "$tabledir/OLS_noncog.csv",   b(3)  se(3) obslast star(* 0.10 ** 0.05 *** 0.01) ///
				keep(treatment_0) ar2 mtitles nonotes title(`var') 	append
			  }
	
			eststo  clear 
		foreach var of varlist $sdq_de{
			eststo clear

			qui	eststo OLS: 	 	reg   	`var'  	treatment_0 $skill_base_de	                  i.countyid_0   i.cohort_0 i.inter_id if paper_sample_main==1, vce(cluster villid_0)  // OLS
			qui	eststo OLSwCont: 	reg   	`var'   treatment_0 $skill_base_de	  $control	      i.countyid_0   i.cohort_0 i.inter_id if paper_sample_main==1, vce(cluster villid_0) // OLS + controls

			esttab using "$tabledir/OLS_noncog_delay.csv",   b(3)  se(3) obslast star(* 0.10 ** 0.05 *** 0.01) ///
				keep(treatment_0) ar2 mtitles nonotes title(`var') 	append
			  }
	
/*------------------------------------------------------------------------------
TABLE 6: Heterogeneous treatment effects on cognitive development
------------------------------------------------------------------------------*/	


   * generate median of mot skill 	 
	xtile skill_medmo = Bayley_mot_rscore_st_0, nq(4) 
	gen skill_medmo_1 = (skill_medmo==1) 
	gen skill_medmo_2 = (skill_medmo==2) 
	gen skill_medmo_3 = (skill_medmo==3) 
	gen skill_medmo_4 = (skill_medmo==4) 
	
	gen skill_medmo_treat = treatment_0*skill_medmo_1
	* generate median of cog skill 	
	xtile skill_med = Bayley_cog_rscore_st_0, nq(4) 
	gen   skill_med_1 = (skill_med==1) 
	gen   skill_med_2 = (skill_med==2) 
	gen   skill_med_3 = (skill_med==3)
	gen   skill_med_4 = (skill_med==4)
	* generate median of distance measure
	xtile delivery_quart   = age_mom_delivery_0, nq(4)
	gen   delivery_quart_1 = (delivery_quart==1) 
	gen   delivery_quart_2 = (delivery_quart==2) 
	gen   delivery_quart_3 = (delivery_quart==3) 
	gen   delivery_quart_4 = (delivery_quart==4) 
	
	
	* generate median of family asset 	
	xtile asset_quart = h_asset1_0, nq(4) 
	gen   asset_quart_1 = (asset_quart==1) 
	gen   asset_quart_2 = (asset_quart==2) 
	gen   asset_quart_3 = (asset_quart==3)
	gen   asset_quart_4 = (asset_quart==4)
	
	* treatment interactions 
	foreach var of varlist skill_med_* delivery_quart_* asset_quart_* {
			gen `var'_treat = treatment_0*`var'
			} 


eststo clear

eststo m1: reg wppsi_FSIQ_raw_st_3 ///
    treatment_0 skill_medmo_treat skill_medmo_1 ///
    i.countyid_0 i.cohort_0 i.wppsi_tid, vce(cluster villid_0)


eststo m2: reg wppsi_FSIQ_raw_st_3 ///
    treatment_0 skill_med_1_treat skill_med_1 ///
    i.countyid_0 i.cohort_0 i.wppsi_tid, vce(cluster villid_0)

eststo m3: reg wppsi_FSIQ_raw_st_3 ///
    treatment_0 delivery_quart_4_treat delivery_quart_4 ///
    i.countyid_0 i.cohort_0 i.wppsi_tid, vce(cluster villid_0)

eststo m4: reg wppsi_FSIQ_raw_st_3 ///
    treatment_0 asset_quart_1_treat asset_quart_1 ///
    i.countyid_0 i.cohort_0 i.wppsi_tid, vce(cluster villid_0)

outreg2 [m1 m2 m3 m4] using "$tabledir/heter.xlsx", replace excel ///
    keep(treatment_0 ///
         skill_medmo_treat skill_medmo_1 ///
         skill_med_1_treat skill_med_1 ///
         delivery_quart_4_treat delivery_quart_4 ///
         asset_quart_1_treat asset_quart_1) ///
    dec(3) ///
    addtext(County FE, Yes, Cohort FE, Yes, Examiner FE, Yes) ///
    addnote("Standard errors clustered at the village level.", ///
            "* p < 0.10, ** p < 0.05, *** p < 0.01.")
		
			
	
			  
/*------------------------------------------------------------------------------
TABLE 7:  Intervention on Parental Investments  - Mediation Analysis
------------------------------------------------------------------------------*/	

	eststo  clear 
	foreach var of varlist $investment{
			eststo clear

			qui	eststo OLS: 	 	reg   	`var'  	treatment_0 	                  i.countyid_0   i.cohort_0  if valid3==1 , vce(cluster villid_0)  // OLS
			
			qui	eststo OLSwCont: 	reg   	`var'   treatment_0 	   $control	      i.countyid_0   i.cohort_0  if valid3==1 , vce(cluster villid_0)  // OLS + controls
			esttab using "$tabledir/mechanism_investment.csv",   b(3)  se(3) obslast star(* 0.10 ** 0.05 *** 0.01) ///
				keep(treatment_0) r2 mtitles nonotes title(`var') 	append
			  }
		  
	
/*------------------------------------------------------------------------------
TABLE 8:  Intervention on Parenting Behaviors  - Mediation Analysis
------------------------------------------------------------------------------*/	
	
    **Table 8a： parenting practice of Primary Caregivers
		eststo  clear 
		      
		qui xi: reg f12_3           treatment_0 f12_0	                    i.countyid_0   i.cohort_0 if valid3==1, vce(cluster villid_0) 	
	    est sto a
		qui xi: reg f13_3           treatment_0 f13_0	                    i.countyid_0   i.cohort_0 if valid3==1, vce(cluster villid_0) 
	    est sto b
		eststo: reg f14_3           treatment_0 f14_0	                    i.countyid_0   i.cohort_0 if valid3==1, vce(cluster villid_0) 
	    est sto c
		eststo: reg f15_3           treatment_0 f15_0                     i.countyid_0   i.cohort_0 if valid3==1, vce(cluster villid_0) 
	    est sto d
		eststo: reg f16_3           treatment_0 f16_0	                    i.countyid_0   i.cohort_0 if valid3==1, vce(cluster villid_0) 
	    est sto e
		eststo: reg f17_3           treatment_0 f17_0                     i.countyid_0   i.cohort_0 if valid3==1, vce(cluster villid_0)  	
	    est sto f
		eststo:reg fa_ppractice 	treatment_0 fa_ppractice_base			i.countyid_0   i.cohort_0 if valid3==1, vce(cluster villid_0)  	
	    est sto g
		
		qui xi: reg f12_3           treatment_0 f12_0	                    $control i.countyid_0   i.cohort_0 if valid3==1, vce(cluster villid_0) 	
	    est sto aa
		qui xi: reg f13_3           treatment_0 f13_0	                    $control i.countyid_0   i.cohort_0 if valid3==1, vce(cluster villid_0) 
	    est sto bb
		eststo: reg f14_3           treatment_0 f14_0	                    $control i.countyid_0   i.cohort_0 if valid3==1, vce(cluster villid_0) 
	    est sto cc
		eststo: reg f15_3           treatment_0 f15_0                     $control i.countyid_0   i.cohort_0 if valid3==1, vce(cluster villid_0) 
	    est sto dd
		eststo: reg f16_3           treatment_0 f16_0	                    $control i.countyid_0   i.cohort_0 if valid3==1, vce(cluster villid_0) 
	    est sto ee
		eststo: reg f17_3           treatment_0 f17_0                     $control i.countyid_0   i.cohort_0 if valid3==1, vce(cluster villid_0)  	
	    est sto ff
		eststo:reg fa_ppractice 	treatment_0 fa_ppractice_base			$control i.countyid_0   i.cohort_0 if valid3==1, vce(cluster villid_0)  	
	    est sto gg


	   outreg2 [a aa b bb  c cc d dd e ee f ff g gg] using "$tabledir/mechanism_practice.xls", excel replace dec(3)    nocons  	///
		               keep(treatment_0 $control) ///
					   title ("Table 3. Average Treatment Effects on parenting practice") ///
				       addnote(All standard errors are clustered at the village level.)										

		
	**Table 8b： parenting practice of father
		eststo  clear 

		qui xi: reg f12_father_3           treatment_0 f12_0	    i.countyid_0   i.cohort_0 if valid3==1, vce(cluster villid_0) 	
	    est sto a
		qui xi: reg f13_father_3           treatment_0 f13_0	    i.countyid_0   i.cohort_0 if valid3==1, vce(cluster villid_0) 
	    est sto b
		eststo: reg f14_father_3           treatment_0 f14_0	    i.countyid_0   i.cohort_0 if valid3==1, vce(cluster villid_0) 
	    est sto c
		eststo: reg f15_father_3           treatment_0 f15_0        i.countyid_0   i.cohort_0 if valid3==1, vce(cluster villid_0) 
	    est sto d
		eststo: reg f16_father_3           treatment_0 f16_0	    i.countyid_0   i.cohort_0 if valid3==1, vce(cluster villid_0) 
	    est sto e
		eststo: reg f17_father_3           treatment_0 f17_0        i.countyid_0   i.cohort_0 if valid3==1, vce(cluster villid_0)  	
	    est sto f
		eststo: reg fa_ppractice_fa 	   treatment_0 fa_ppractice_base	     i.countyid_0   i.cohort_0 if valid3==1, vce(cluster villid_0)  	
	    est sto g

		qui xi: reg f12_father_3           treatment_0 f12_0	     $control  i.countyid_0   i.cohort_0 if valid3==1, vce(cluster villid_0) 	
	    est sto aa
		qui xi: reg f13_father_3           treatment_0 f13_0	     $control  i.countyid_0   i.cohort_0 if valid3==1, vce(cluster villid_0) 
	    est sto bb
		eststo: reg f14_father_3           treatment_0 f14_0	     $control  i.countyid_0   i.cohort_0 if valid3==1, vce(cluster villid_0) 
	    est sto cc
		eststo: reg f15_father_3           treatment_0 f15_0         $control  i.countyid_0   i.cohort_0 if valid3==1, vce(cluster villid_0) 
	    est sto dd
		eststo: reg f16_father_3           treatment_0 f16_0	     $control  i.countyid_0   i.cohort_0 if valid3==1, vce(cluster villid_0) 
	    est sto ee
		eststo: reg f17_father_3           treatment_0 f17_0         $control  i.countyid_0   i.cohort_0 if valid3==1, vce(cluster villid_0)  	
	    est sto ff
		eststo: reg fa_ppractice_fa 	   treatment_0 fa_ppractice_base     $control	 i.countyid_0   i.cohort_0 if valid3==1, vce(cluster villid_0)  	
	    est sto gg

		
	   outreg2 [a aa b bb  c cc d dd e ee f ff g gg] using "$tabledir/mechanism_practice_father.xls", excel replace dec(3)  label  nocons  	///
		               keep(treatment $control ) ///
					   title ("Table 3. Average Treatment Effects on parenting practice of father") ///
				       addnote(All standard errors are clustered at the village level.)										

					   
/*------------------------------------------------------------------------------
TABLE 9:  Intervention on Parents' Education Expectations  - Mediation Analysis
------------------------------------------------------------------------------*/					 
	   
		gen      exp_edu_mdummy=1 if exp_edu_level_3<=4
		replace  exp_edu_mdummy=2 if exp_edu_level_3==5 
		replace  exp_edu_mdummy=3 if exp_edu_level_3==6
		replace  exp_edu_mdummy=4 if exp_edu_level_3==7 | exp_edu_level_3==8
			
		gen      exp_edu_dummy=0 if exp_edu_level_3<=4
		replace  exp_edu_dummy=1 if exp_edu_level_3>4 &  exp_edu_level_3!=.

		 
		eststo  clear 
			 
		eststo:reg  exp_edu_dummy     treatment_0 			           i.countyid_0   i.cohort_0 if valid3==1, vce(cluster villid_0)  	
	    est sto a

		eststo:reg  exp_edu_dummy     treatment_0 			$control   i.countyid_0   i.cohort_0 if valid3==1, vce(cluster villid_0)  	
	    est sto aa
		
	    outreg2 [a aa] using "$tabledir/mechanism_exp_edu.xls", excel replace dec(3)    nocons  	///
	 	               keep(treatment) ///
					   title ("Table 3. Average Treatment Effects on education expectation") ///
				       addnote(All standard errors are clustered at the village level.)										
			 
/*------------------------------------------------------------------------------
TABLE 10:  Intervention on Parents' School Choices  - Mediation Analysis
------------------------------------------------------------------------------*/					 
	   
	     recode kg_type_3 (2=0)(3=0)
	
			eststo  clear 
		foreach var of varlist kg_level_du kg_type_3 move_3 {
			eststo clear

			qui	eststo OLS: 	 	reg   	`var'  	treatment_0 	                i.countyid_0  i.villid_0  i.cohort_0  if valid3==1 , vce(cluster villid_0)  // OLS
			
			qui	eststo OLSwCont: 	reg   	`var'   treatment_0 	   $control	    i.countyid_0  i.villid_0   i.cohort_0  if valid3==1 , vce(cluster villid_0)  // OLS + controls
			esttab using "$tabledir/mechanism_kg.csv",   b(3)  se(3) obslast star(* 0.10 ** 0.05 *** 0.01) ///
				keep(treatment_0) r2 mtitles nonotes title(`var') 	append
			  }
		  
/*------------------------------------------------------------------------------
TABLE 11:  Intervention on  Caregivers' Mental Health  - Mediation Analysis
------------------------------------------------------------------------------*/	
		
		eststo  clear 
			 
		qui xi: reg depression_score_3        treatment_0 	        i.countyid_0   i.cohort_0 if valid3==1, vce(cluster villid_0) 	
	    est sto a
		qui xi: reg anxiety_score_3           treatment_0	        i.countyid_0   i.cohort_0 if valid3==1, vce(cluster villid_0) 
	    est sto b
		eststo: reg stress_score_3            treatment_0	        i.countyid_0   i.cohort_0 if valid3==1, vce(cluster villid_0) 
	    est sto c

		qui xi: reg depression_score_3        treatment_0 	        $control    i.countyid_0   i.cohort_0 if valid3==1, vce(cluster villid_0) 	
	    est sto aa
		qui xi: reg anxiety_score_3           treatment_0	        $control    i.countyid_0   i.cohort_0 if valid3==1, vce(cluster villid_0)
	    est sto bb
		eststo: reg stress_score_3            treatment_0	        $control    i.countyid_0   i.cohort_0 if valid3==1, vce(cluster villid_0) 
	    est sto cc

	   outreg2 [a aa b bb c cc] using "$tabledir/mechanism_dass1.xls", excel replace dec(3)    nocons  	///
		               keep(treatment pri_caregiver_mom_3 pri_caregiver_par_3 $control) ///
					   title ("Table 3. Average Treatment Effects on parent mental health") ///
				       addnote(All standard errors are clustered at the village level.)										
		
		gen depression_dummy=1 if depression_de_3>1 & depression_de_3!=.
		gen anxiety_dummy=1 if anxiety_de_3>1 & anxiety_de_3!=.
		gen stress_dummy=1 if stress_de_3>1 & stress_de_3!=.
		
		replace depression_dummy=0 if depression_de_3==1
		replace anxiety_dummy=0 if anxiety_de_3==1
		replace stress_dummy=0 if stress_de_3==1

		
		eststo: reg depression_dummy           treatment_0            i.countyid_0   i.cohort_0 if valid3==1, vce(cluster villid_0) 
	    est sto d
		eststo: reg anxiety_dummy              treatment_0	          i.countyid_0   i.cohort_0 if valid3==1, vce(cluster villid_0) 
	    est sto e 
		eststo: reg stress_dummy               treatment_0            i.countyid_0   i.cohort_0 if valid3==1, vce(cluster villid_0)  	
	    est sto f

		eststo: reg depression_dummy           treatment_0        $control     i.countyid_0   i.cohort_0 if valid3==1, vce(cluster villid_0) 
	    est sto dd
		eststo: reg anxiety_dummy              treatment_0	      $control     i.countyid_0   i.cohort_0 if valid3==1, vce(cluster villid_0) 
	    est sto ee 
		eststo: reg stress_dummy               treatment_0        $control     i.countyid_0   i.cohort_0 if valid3==1, vce(cluster villid_0)  	
	    est sto ff

		
	   outreg2 [d dd e ee f ff] using "$tabledir/mechanism_dass2.xls", excel replace dec(3)    nocons  	///
		               keep(treatment pri_caregiver_mom_3 pri_caregiver_par_3 $control) ///
					   title ("Table 3. Average Treatment Effects on parent mental health") ///
				       addnote(All standard errors are clustered at the village level.)	
	
	